#include <cstdlib>
#include <ctime>

#include "Random.hpp"

namespace fge
{

Random::Random(uint32_t seed)
{
	if(seed == 0)
		seed = static_cast<uint32_t>(time(NULL));

	srand(seed);

	// MAX_RAND is only guaranteed to be 16 bits, we'll generate 32 bit random
	// numbers by combining two for each number
	x = static_cast<uint32_t>(rand()) << 16 | rand();
	y = static_cast<uint32_t>(rand()) << 16 | rand();
	z = static_cast<uint32_t>(rand()) << 16 | rand();
	w = static_cast<uint32_t>(rand()) << 16 | rand();
	v = static_cast<uint32_t>(rand()) << 16 | rand();
}

int32_t Random::NextInt()
{
	return xorshift();
}

int32_t Random::NextInt(int32_t maximum)
{
	return NextInt() % maximum;
}

int32_t Random::NextInt(int32_t minimum, int32_t maximum)
{
	return NextInt(maximum - minimum) + minimum;
}

float Random::NextFloat()
{
	return NextInt(32768) / 32768.0f;
}

float Random::NextFloat(float maximum)
{
	return NextFloat() * maximum;
}

float Random::NextFloat(float minimum, float maximum)
{
	return NextFloat(maximum - minimum) + minimum;
}

}
